import { Inject, Injectable, NotFoundException } from "@nestjs/common";
import { Page } from "./entities/page.entity";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { PostgresqlPersistence } from "y-postgresql";
import { yjsXmlMentionCollect } from "src/utils/yjsXmlMentionCollect";

@Injectable()
export class PageService {
  constructor(
    @InjectRepository(Page)
    private readonly pageRepository: Repository<Page>,
    @Inject("YJS_POSTGRESQL_ADAPTER") private readonly yjsPostgresqlAdapter: PostgresqlPersistence
  ) {}

  async create(payload) {
    this.pageRepository.save(payload);
    return payload;
  }

  async update(payload) {
    const res = await this.pageRepository.update({ pageId: payload.pageId }, payload);

    if (res.affected === 0) {
      return new NotFoundException("page not found");
    }

    return payload;
  }

  async fetch(params: { pageId: string; userId: number }) {
    // 只有创建Page的用户才能访问
    // const page = await this.pageRepository.findOne({
    //   where: { pageId: params.pageId, user: { id: params.userId } }
    // });

    const page = await this.pageRepository.findOne({
      where: { pageId: params.pageId }
    });

    if (!page) {
      return new NotFoundException("page not found");
    }

    return page;
  }

  async list(params: { userId: number }) {
    // 根据创建用户查询Page列表
    // const [data, count] = await this.pageRepository.findAndCount({
    //   where: { user: { id: params.userId } },
    //   order: { createdAt: "DESC" }
    // });

    // 查询所有Page列表
    const [data, count] = await this.pageRepository.findAndCount({
      order: { createdAt: "DESC" }
    });

    return {
      pages: data,
      count
    };
  }

  async delete(payload: { pageId: string; userId: number }) {
    const res = await this.pageRepository.delete({ pageId: payload.pageId, user: { id: payload.userId } });

    if (res.affected === 0) {
      return new NotFoundException("page not found");
    }

    return res.raw[0];
  }

  async graph() {
    const pages = await this.pageRepository.find();
    const doc = await this.yjsPostgresqlAdapter.getYDoc("ws-yjs");
    const withLinksPages = pages.map(page => {
      const pageDoc = doc.getXmlElement(`document-store-${page.pageId}`).toJSON();
      if (pageDoc) {
        return {
          ...page,
          links: yjsXmlMentionCollect(pageDoc)
        };
      }
    });

    return withLinksPages;
  }
}
